home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 Reader Awards / macformat-098.iso / MF Data / Behaviours / 00002_Script_Turn Towards Mouse < prev    next >
Encoding:
Text File  |  2000-11-01  |  6.8 KB  |  221 lines

  1. -- DESCRIPTION --
  2.  
  3. on getBehaviorDescription me
  4.   return \
  5.     "TURN TOWARDS MOUSE" & RETURN & RETURN & \
  6.     "Orients a sprite to face (or turn away from) the mouse pointer, even if the sprite is moving." & RETURN & RETURN & \
  7.     "You can choose whether the sprite always turns to face the mouse, or only when the mouse button is up or down. " & \
  8.     "When the mouse is in the other state, the will either return to its initial position, or remain in the position it was in when the mouse state changed." & RETURN & RETURN & \
  9.     "The graphic member is considered to face right when it is at rest." & RETURN & RETURN & \
  10.     "PERMITTED MEMBER TYPES:" & RETURN & \
  11.     "bitmap, Flash, vector shape" & RETURN & RETURN & \
  12.     "PARAMETERS:" & RETURN & \
  13.     "* Whether to turn toward or away from the mouse" & RETURN & \
  14.     "* When to turn (always, while mouse is up, or while mouse is down)" & RETURN & \
  15.     "* Whether to return to initial position or remain in new position"
  16. end getBehaviorDescription
  17.  
  18.  
  19. on getBehaviorTooltip me
  20.   return \
  21.     "Use with bitmap, Flash and vector shape members." & RETURN & RETURN & \
  22.     "Orients a sprite to face towards or away from the mouse pointer, even if the sprite is moving." & RETURN & RETURN & \
  23.     "You can make the sprite face the mouse at all times, or only when the mouse is up or down. " & \
  24.     "When the mouse is in the other state, the sprite can either revert to its original postion, or remain in the position it was in when the mouse state changed. " & \
  25.     "The member is considered to face right at rest."
  26. end getBehaviorTooltip
  27.  
  28.  
  29.  
  30. -- NOTES FOR DEVELOPERS --
  31.  
  32. -- The main handler is Turn me.  This simply caclulates the difference
  33. -- between the loc of the sprite and the mouseLoc.  It then asks the custom
  34. -- GetAngle function to convert this into an angle expressed in degrees.
  35. -- GetAngle will return an angle of zero if the mySpriteToFace is on a
  36. -- horizontal line to the right of mySprite. The angle increases clockwise.
  37.  
  38. -- Director 7 treats trigonometry in radians and sprite rotation in degrees.
  39.  
  40. -- Turn me is only called if the behavior is active.  Just when this is
  41. -- is determined by the myActivePeriod parameter.  In order to make the
  42. -- choices clear in the getPropertyDescriptionList handler, 
  43. -- myActivePeriod is initially in the form of a string.  A string is slow to
  44. -- handle, and the data it contains will need to be consulted on every
  45. -- prepareFrame.  In order to optimize performance, the strings are
  46. -- converted to symbols in the Initialize handler.  Symbols are simply
  47. -- integers disguised as a single word.  They are thus both fast and
  48. -- expressive.
  49.  
  50. -- The Activate handler first tests if the mouse has been either clicked or
  51. -- released, and then runs through a 'case' statement to return a TRUE | FALSE
  52. -- value depending on the mouse's current state.  IF the value returned is
  53. -- TRUE, then the Turn will be called.
  54.  
  55.  
  56.  
  57. -- HISTORY --
  58.  
  59. -- 14 September 1998: written for the D7 Behaviors Palette by James Newton
  60. -- 3 November 1998:   descriptions improved
  61. -- 7 January 2000:    isOkToAttach added J.Powers | String Keys
  62.  
  63.  
  64.  
  65. -- PROPERTIES --
  66.  
  67. property mySprite    -- error checking
  68. property getPDLError    -- author-defined parameters
  69. property myOrientation  -- TRUE if sprite turns away from mouse
  70. property myActivePeriod -- #always | #mouseDown | #mouseUp | #mouseRelease | #mouseClick
  71. property myResetFlag    -- TRUE if sprite is to revert to its initial position
  72. property myInitialAngle -- Angle of sprite at start of span
  73. property myMouseDown    -- TRUE while the mouse is down
  74.  
  75.  
  76. -- EVENT HANDLERS --
  77.  
  78. on beginSprite me
  79.   Initialize me
  80. end beginSprite
  81.  
  82.  
  83. on prepareFrame me
  84.   if Activate (me) then
  85.     Turn me
  86.   else if myResetFlag then
  87.     if mySprite.rotation <> myInitialAngle then
  88.       mySprite.rotation = myInitialAngle
  89.     end if
  90.   end if
  91. end prepareFrame
  92.  
  93.  
  94.  
  95. -- CUSTOM HANDLERS --
  96.  
  97. on Initialize me -- sent by beginSprite
  98.   mySprite = sprite(me.spriteNum)
  99.   memberType = mySprite.member.type
  100.   
  101.   case memberType of
  102.     #flash, #vectorShape: mySprite.scaleMode = #autoSize
  103.   end case
  104.   
  105.   myMouseDown = the stilldown
  106.   myInitialAngle = mySprite.rotation
  107.   -- Convert strings
  108.   myOrientation = (myOrientation = "away from the mouse")
  109.   case myActivePeriod of
  110.     "always":                     myActivePeriod = #always
  111.     "when the mouse is clicked":  myActivePeriod = #mouseClick
  112.     "when the mouse is released": myActivePeriod = #mouseRelease
  113.     "while the mouse is down":    myActivePeriod = #mouseDown
  114.     "while the mouse is up":      myActivePeriod = #mouseUp
  115.   end case
  116.   myResetFlag = (myResetFlag = "return to the initial position")
  117. end Initialize
  118.  
  119. on Activate me
  120.   newStatus = (myMouseDown <> (the stilldown))
  121.   if newStatus then
  122.     myMouseDown = (the stilldown)
  123.   end if
  124.   
  125.   case myActivePeriod of
  126.     #always:       return TRUE
  127.     #mouseClick:   return newStatus AND (the stilldown)
  128.     #mouseRelease: return newStatus AND (not the stilldown)
  129.     #mouseDown:    return (the stilldown)
  130.     #mouseUp:      return (not the stilldown)
  131.   end case
  132. end Activate
  133.  
  134.  
  135. on Turn me
  136.   slope = the mouseLoc - mySprite.loc
  137.   angle = GetAngle (me, slope)
  138.   if myOrientation then
  139.     angle = angle + 180
  140.   end if
  141.   if mySprite.rotation <> angle then
  142.     mySprite.rotation = angle
  143.   end if
  144. end Turn
  145.  
  146.  
  147. on GetAngle me, slope
  148.   deltaH = slope[1]
  149.   deltaV = slope[2]
  150.   if deltaH then
  151.     slope = float (deltaV) / deltaH 
  152.     angle = atan (slope)
  153.     if deltaH  < 0 then
  154.       angle = angle + pi
  155.     end if
  156.   else if deltaV > 0 then
  157.     angle = pi / 2
  158.   else if deltaV < 0 then
  159.     angle = (3 * pi) / 2
  160.   else
  161.     angle = 0
  162.   end if
  163.   -- Convert to degrees for .rotation
  164.   angle = (angle * 180) / pi
  165.   
  166.   return angle
  167. end GetAngle
  168.  
  169.  
  170.  
  171. -- AUTHOR-DEFINED PARAMETERS --
  172.  
  173. on isOKToAttach (me, aSpriteType, aSpriteNum)
  174.   case aSpriteType of
  175.     #Graphic:
  176.       return getPos([#bitmap, #flash, #vectorShape],sprite(aSpriteNum).member.type)<>0
  177.     #script:
  178.       return FALSE
  179.   end case
  180. end
  181.  
  182.  
  183. on getPropertyDescriptionList me
  184.   
  185.   if not the currentSpriteNum then exit
  186.   
  187.   return \
  188. [ \
  189.  #myOrientation: \
  190.  [ \
  191.   #comment: "Turn", \
  192.   #format:  #string, \
  193.   #range:  ["towards the mouse", "away from the mouse"], \
  194.   #default: "towards the mouse" \
  195.  ], \
  196.  #myActivePeriod: \
  197.  [ \
  198.   #comment: EMPTY, \
  199.   #format: #string, \
  200.   #range: \
  201.   [ \
  202.    "while the mouse is down", \
  203.    "while the mouse is up", \
  204.    "when the mouse is clicked", \
  205.    "when the mouse is released", \
  206.    "always" \
  207.   ], \
  208.   #default:  "while the mouse is down" \
  209.  ], \
  210.  #myResetFlag: \
  211.  [ \
  212.   #comment: "Otherwise", \
  213.   #format:  #string, \
  214.   #range:  ["return to the initial position", "remain in the new position"], \
  215.   #default: "return to the initial position" \
  216.  ] \
  217. ]
  218. end getPropertyDescriptionList
  219.  
  220.  
  221.